
#include <stdio.h>
#include <time.h>
#include <math.h>

long add_1(long n)
{
    long i, sum = 0;
    for (i = 1; i <= n; i++)
        sum += i;
    return sum;
}

long add_2(long n)
{
    return n * (n + 1) / 2;
}

void time1(long n)
{
    clock_t t;
    long sum;
    t = clock();
    sum = add_1(n);
    t = clock() - t;
    printf("方法1:\n");
    printf("计算1 ~ %ld结果为:%ld\n", n, sum);
    printf("方法1的用时为:%lf秒\n", ((float)t / CLOCKS_PER_SEC));
}

void time2(long n)
{
    clock_t t;
    long sum;
    t = clock();
    sum = add_2(n);
    t = clock() - t;
    printf("方法2:\n");
    printf("计算2 ~ %ld结果为:%ld\n", n, sum);
    printf("方法2的用时为:%lf秒\n", ((float)t / CLOCKS_PER_SEC));


}
int main()
{
    long n;
    printf("n(大于1000000):\n");
    printf("输入n的值:");
    scanf("%ld", &n);
    if (n < 1000000)
        return 0;
    time1(n);
    time2(n);
    return 1;
}